package cn.kasa.org.service.impl;

import cn.kasa.basic.service.impl.BaseServiceImpl;
import cn.kasa.basic.utils.PageList;
import cn.kasa.org.domain.Department;
import cn.kasa.org.domain.Employee;
import cn.kasa.org.domain.Employee;
import cn.kasa.org.mapper.EmployeeMapper;
import cn.kasa.org.query.EmployeeQuery;
import cn.kasa.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* @Description: service实现类
* @Author: LiuXinYu
* @Version: 1.0
* @Date:  2022/6/21 18:36
*/

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;



    @Override
    public List<Employee> tree() {
        // 查出所有部门数据
        List<Employee> emps = employeeMapper.loadAll();
        // 创建一个集合对象，放树数据
        List<Employee> empTree = new ArrayList<Employee>();
        // 将集合数据转换为map，解决时间复杂度问题
        HashMap<Long, Employee> empMap = new HashMap<>();
        for (Employee employee : emps){
            empMap.put(employee.getId(), employee);
        }
        // 从所有数据中获取一级数据
        for (Employee empTmp :emps){
            if (empTmp.getDepartment_id() == null){
                empTree.add(empTmp);
            }else{
                Employee employee = empMap.get(empTmp.getDepartment_id());
                if (employee != null){
                    employee.getChildren().add(empTmp);
                }
            }
        }

        return empTree;
    }
}
